{extend name="public:admin_form" /}

{block name="body"}
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">

                <div class="layui-form-item">
                  <label class="layui-form-label">上级分类</label>
                  <div class="layui-input-block">
                    <div id="menuSelectBox" class="ew-xmselect-tree"></div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">分类名</label>
                  <div class="layui-input-block">
                    <input type="text" name="catename" lay-verify="required" placeholder="分类名*" autocomplete="off" class="layui-input">
                  </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">EN别名</label>
                  <div class="layui-input-block">
                    <input type="text" name="ename" lay-verify="required" placeholder="英文名*" autocomplete="off" class="layui-input">
                  </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">图标</label>
                  <div class="layui-input-block">
                    <input type="text" name="icon" placeholder="图标*" id="iconPicker" lay-filter="iconPicker" style="display:none;" class="layui-input">
                  </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">详情页模板</label>
                    <div class="layui-input-block">
                        <select name="detpl" id="tpl" lay-verify="required">
                            {volist name="template" id="vo"}
                            <option value="{$vo}" >{$vo}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">描述</label>
                  <div class="layui-input-block">
                    <textarea type="text" name="desc" lay-verify="required" placeholder="描述*" autocomplete="off" class="layui-textarea"></textarea>
                  </div>
                </div>
                <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                  <div class="layui-input-block">
                    <input type="text" name="sort" lay-verify="number|required" placeholder="请填数字" autocomplete="off" class="layui-input">
                  </div>
                </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="cate-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{/block}

{block name="js"}
  <script>
  layui.use(['form', 'iconPicker', 'xmSelect',], function(){
    var $ = layui.jquery
    ,form = layui.form ;
	var iconPicker = layui.iconPicker;
    var xmSelect = layui.xmSelect;
    var initPid = 0;

    let ADD_EDIT = "{:url('content.cate/addEdit')}";
	
	//初始化图标选择
	iconPicker.render({
		elem: '#iconPicker',
		type: 'fontClass',
		search: false,
		page: true,
		limit: 12,
		click: function (data) {
			//console.log(data);
		}
	});

      form.on('submit(cate-save)', function(data) {
          $.ajax({
              url: ADD_EDIT,
              data: JSON.stringify(data.field),
              dataType: 'json',
              contentType: 'application/json',
              type: 'post',
              success: function(result) {
                  if (result.code === 0) {
                      layer.msg(result.msg, {
                          icon: 1,
                          time: 1000
                      }, function() {
                          // parent.layui.table.reload("cate-table");
                          parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                          window.parent.location.reload();
                      });
                  } else {
                      layer.msg(result.msg, {
                          icon: 2,
                          time: 1000
                      });
                  }
              }
          })
          return false;
      });

      (function (){
          //分类菜单结构
          $.ajax({
              type: "get",
              url: "{:url('content.forum/getCateTree')}",
              success: function(res){
                  var data = res.data;

                  // 渲染下拉树
                  xmSelect.render({
                      el: '#menuSelectBox',
                      name: 'pid',
                      height: '250px',
                      layVerify: 'required',
                      layVerType: 'tips',
                      data: data,
                      initValue:  [initPid],
                      model: {label: {type: 'text'}},
                      prop: {
                          name: 'catename',
                          value: 'id'
                      },
                      radio: true,
                      clickClose: true,
                      tree: {
                          show: true,
                          indent: 15,
                          strict: false,
                          expandedKeys: true
                      },
                      tips: '请选择上级菜单'
                  });


                  // var demo4 = xmSelect.render({
                  //   el: '#menuSelectBox',
                  //   model: { label: { type: 'text' } },
                  //   tree: {
                  //     show: true,
                  //     strict: false,
                  //     expandedKeys: [ -1 ],
                  //   },
                  //   on: function(data){
                  //     if(data.isAdd){
                  //       return data.change.slice(0, 1)
                  //     }
                  //   },
                  //   height: 'auto',
                  // })

                  // //这里模拟ajax
                  // setTimeout(function(){
                  //   demo4.update({
                  //     data: [
                  //       {name: '销售员', value: -1, children: [
                  //         {name: '张三', value: 100, children: []},
                  //         {name: '李四1', value: 2},
                  //         {name: '王五1', value: 3, disabled: true},
                  //       ]},
                  //       {name: '奖品', value: -2, children: [
                  //         {name: '奖品3', value: -3, children: [
                  //           {name: '苹果3', value: 14},
                  //           {name: '香蕉3', value: 15},
                  //           {name: '葡萄3', value: 16},
                  //         ]},
                  //         {name: '苹果2', value: 4, disabled: true},
                  //         {name: '香蕉2', value: 5},
                  //         {name: '葡萄2', value: 6},
                  //       ]},
                  //     ]
                  //   })

                  //   //设置默认值
                  //   demo4.setValue([
                  //     {name: '李四1', value: 2},
                  //     {name: '苹果3', value: 14},
                  //   ], null, true)

                  // }, 300)

              }

          });
      })()


  })
  </script>
{/block}